package com.gz.jdbc;

import java.sql.*;
import java.util.Scanner;

public class Jdbc03 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Scanner sc = new Scanner(System.in);
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/a0327";
        String username = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url, username, password);

        String sql="insert into bank values(?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        System.out.println("请输入要添加的id");
        String id=sc.nextLine();
        System.out.println("请输入要添加的b1");
        String b1=sc.nextLine();

        preparedStatement.setString(1,id);
        preparedStatement.setInt(2,Integer.parseInt(b1));
        int i = preparedStatement.executeUpdate();//影响行数
        System.out.println(i>0?"执行成功":"执行失败");

        sql="select * from bank";
        preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
             id=resultSet.getString("id");//获取该行第1列
             int b2=resultSet.getInt("b1");//获取该行第2列

            System.out.println(id+"\t"+b2);

        }

        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
